# Maintainer: Alexey Pavlov <alexpux@gmail.com>

# Before building package need CD into directory with drive letter, like
# cd /c/build/mingw-w64-ruby

# be sure to run the updpkgprovs and rebuild.  This is to get the propper version
# of the ruby gems that were bundled.

_realname=ruby
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.7.1
pkgrel=2
pkgdesc="An object-oriented language for quick and easy programming (mingw-w64)"
arch=('any')
url="https://www.ruby-lang.org/en"
license=("BSD, custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" 
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-graphviz"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gdbm"
         "${MINGW_PACKAGE_PREFIX}-libyaml"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-pdcurses"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-tk")
options=('staticlibs' 'strip')
source=("https://cache.ruby-lang.org/pub/ruby/${pkgver%.*}/${_realname}-${pkgver}.tar.bz2"
        0001-mingw-w64-time-functions.patch
        0003-fix-check-types.patch
        0004-rbinstall-destdir.patch
        0005-rbinstall-rake-bash.patch)
## Populated by the updpkgprovs script
provides=(
    "${MINGW_PACKAGE_PREFIX}-ruby-did_you_mean=1.3.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-minitest=5.11.3"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-telnet=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-power_assert=1.1.3"
    "${MINGW_PACKAGE_PREFIX}-ruby-rake=12.3.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-test-unit=3.2.9"
    "${MINGW_PACKAGE_PREFIX}-ruby-xmlrpc=0.3.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-bigdecimal=1.4.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-bundler=1.17.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-cmath=1.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-csv=3.0.9"
    "${MINGW_PACKAGE_PREFIX}-ruby-date=2.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-dbm=1.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-e2mmap=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-etc=1.0.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-fcntl=1.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-fiddle=1.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-fileutils=1.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-forwardable=1.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-gdbm=2.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-io-console=0.4.7"
    "${MINGW_PACKAGE_PREFIX}-ruby-ipaddr=1.2.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-irb=1.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-json=2.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-logger=1.3.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-matrix=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-mutex_m=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-openssl=2.1.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-ostruct=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-prime=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-psych=3.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-rdoc=6.1.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-rexml=3.1.9"
    "${MINGW_PACKAGE_PREFIX}-ruby-rss=0.2.7"
    "${MINGW_PACKAGE_PREFIX}-ruby-scanf=1.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-sdbm=1.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-shell=0.7"
    "${MINGW_PACKAGE_PREFIX}-ruby-stringio=0.0.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-strscan=1.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-sync=0.5.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-thwait=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-tracer=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-webrick=1.4.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-zlib=1.0.0"
)
## End of pkgupdprovs modifications

sha256sums=('d703d58a67e7ed822d6e4a6ea9e44255f689a5b6ea6752d17e8d031849822202'
            '329994d3bf7e692e18c1faffbedfbd076e5d00257b2100387a773b59b81ccd4e'
            'cbb083aece8e284fb1ae625e689f044a608aa9750bba9f7d399e895547eee39b'
            '02382ec3b9e42d7dbb58edad3e41c361d98871711bb2f0320082c2acc6a82e2e'
            '9fb5acb995255eedde0f0b0bf5cf2c02f00dc088d2538ff9ebb69627aafd1e42')
noextract=(${_realname}-${pkgver}.tar.bz2)

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xjf ${srcdir}/${_realname}-${pkgver}.tar.bz2 -C ${srcdir} || true

  cd ${srcdir}/${_realname}-${pkgver}
  # Pthread disabled in ruby configure for mingw
  # 0001-mingw-w64-time-functions.patch
  apply_patch_with_msg \
      0003-fix-check-types.patch \
      0005-rbinstall-rake-bash.patch
    #  patch -p1 -i ${srcdir}/0004-rbinstall-destdir.patch
  autoreconf -fi
}

build() {

  local FFI_INC=$(pkg-config --cflags libffi)
  CPPFLAGS+=" -DFD_SETSIZE=2048 ${FFI_INC} -I${MINGW_PREFIX}/include/pdcurses"
  CFLAGS+=" -I${MINGW_PREFIX}/include/pdcurses ${FFI_INC}"
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/pdcurses ${FFI_INC}"

  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}
  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make test || true
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install-nodoc
  for script in {erb,gem,irb,rdoc,ri}; do
    install ${srcdir}/ruby-${pkgver}/bin/${script} \
      ${pkgdir}${MINGW_PREFIX}/bin/
  done
  local PREFIX_WIN
# fixup rake.bat so that it can be invoked from "cmd"
  PREFIX_WIN=$(cygpath -wm ${pkgdir}${MINGW_PREFIX})
  sed -e "s|\\\|/|g" -e "s|${MINGW_PREFIX}/bin/ruby.exe|ruby.exe|g" \
      -e "s|${PREFIX_WIN}/bin/rake|rake|g" -i "${pkgdir}${MINGW_PREFIX}/bin/rake.bat"

  install -D -m644 ${srcdir}/${_realname}-${pkgver}/COPYING \
     "${pkgdir}${MINGW_PREFIX}/share/licenses/ruby/LICENSE"
  install -D -m644 ${srcdir}/${_realname}-${pkgver}/BSDL \
     "${pkgdir}${MINGW_PREFIX}/share/licenses/ruby/BSDL"
}
